﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SantaSLittleIrcHelper.Irc;
using System.Drawing;

namespace SantaSLittleIrcHelper.Registration
{
    class ConfirmationProcess : Process
    {

        public override void start()
        {
            this.feedbackLabel = (TLabel)Program.instance.GUI.Controls.Find("labelConfirmationFeedback", true)[0];
            if (!Program.instance.IRC.IsConnected)
            {
                this.invokeSetFeedbackMessage(Color.Orange, Languages.Instance.get("connecting"));
                Program.instance.connect();
                return;
            }
            this.invokeSetFeedbackMessage(Color.Orange, Languages.Instance.get("processRunning"));


            Settings s = Program.instance.settings;
            if (String.IsNullOrEmpty(s.nsUsername))
            {
                Program.instance.consoleWrite("Please enter the username you want to confirm.");
                Program.instance.killProcess();
                this.invokeSetFeedbackMessage(Color.Red, Languages.Instance.get("confirmationErrorMissingUsername", s.nsUsername));
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameNotSuccessfulConfirmedMissingUsername);
                return;
            }
            else if (String.IsNullOrEmpty(s.nsConfirmation))
            {
                Program.instance.consoleWrite("Please enter the confirmation code or the line with the confirmation code.");
                Program.instance.killProcess();
                this.invokeSetFeedbackMessage(Color.Red, Languages.Instance.get("confirmationErrorMissingConfirmationCode", s.nsUsername));
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameNotSuccessfulConfirmedMissingConfirmationCode);
                return;
            }
            Program.instance.IRC.WriteLine(Rfc2812.Nick(s.nsUsername));
            this.step = new StepCallback(this.renamed);
            this.triggerString = Program.instance.nickname;

        }

        public void renamed(String msg)
        {
            Settings s = Program.instance.settings;
            if (msg.Contains("Nickname is already in use"))
            {
                Program.instance.consoleWrite("The name is already in use. Registration cannot be finished when the name is not free.");
                this.invokeSetFeedbackMessage(Color.Red, Languages.Instance.get("confirmationErrorNicknameInUse", s.nsUsername));
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameNotSuccessfulConfirmedNicknameInUse);
                Program.instance.killProcess();
                return;
            }

            String c = s.nsConfirmation;
            String code;
            if (c.Contains("confirm"))
            {
                code = c.Substring(c.IndexOf("confirm") + 8, c.IndexOf(" ", c.IndexOf("confirm") + 8) - c.IndexOf("confirm") - 8);
            }
            else
            {
                code = c;
            }

            Program.instance.IRC.WriteLine(Rfc2812.Privmsg("nickserv", "confirm " + code));
            triggerString = "NickServ";
            step = new StepCallback(this.confirmed);

        }

        private void confirmed(String msg)
        {
            Settings s = Program.instance.settings;
            if (msg.Contains("Invalid passcode"))
            {
                Program.instance.consoleWrite("The passcode you entered has not been accepted.");
                this.invokeSetFeedbackMessage(Color.Red, Languages.Instance.get("confirmationErrorPasscodeInvalid", s.nsUsername));
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameNotSuccessfulConfirmedPasscodeInvalid);
                Program.instance.killProcess();
            }
            else if (msg.Contains("Nickname " + s.nsUsername + " registered"))
            {
                Program.instance.consoleWrite("The username has been confirmed successfully. You can now use it");
                this.invokeSetFeedbackMessage(Color.Green, Languages.Instance.get("confirmationErrorPasscodeInvalid", s.nsUsername));
                RegistrationAssistant.Instance.stateChanged(lastActionDone.NicknameSuccessfulConfirmed);
                Program.instance.IRC.WriteLine(Rfc2812.Nick(Program.instance.nickname));
                this.step = new StepCallback(this.renamedBack);
                this.triggerString = "NICK";
            }
        }

        public void renamedBack(String msg)
        {
            if (msg.Contains(Program.instance.nickname))
            {
                Program.instance.consoleWrite("Process done.");
                Program.instance.killProcess();
            }
        }



    }
}
